﻿/* 
    Copyright (c) 2011 Microsoft Corporation.  All rights reserved.
    Use of this sample source code is subject to the terms of the Microsoft license 
    agreement under which you licensed this sample source code and is provided AS-IS.
    If you did not accept the terms of the license agreement, you are not authorized 
    to use this sample source code.  For the terms of the license, please see the 
    license agreement between you and Microsoft.
  
    To see all Code Samples for Windows Phone, visit http://go.microsoft.com/fwlink/?LinkID=219604 
  
*/
using System;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace EnglishStudy.Entity
{

    public class LessonDataContext : DataContext
    {
        // Pass the connection string to the base class.
        public LessonDataContext(string connectionString)
            : base(connectionString)
        { }

        // Specify a table for the to-do items.
        public Table<LessonItem> LessonItems;

        // Specify a table for the categories.
        public Table<LessonCategory> Categories;

        public Table<WordTable> WordList;

        public Table<Country> Countries;

        public Table<User> usertable;
    }

    [Table]
    public class LessonItem : INotifyPropertyChanged, INotifyPropertyChanging
    {

        // Define ID: private field, public property, and database column.
        private int _lessonItemId;

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int LessonItemId
        {
            get { return _lessonItemId; }
            set
            {
                if (_lessonItemId != value)
                {
                    NotifyPropertyChanging("LessonItemId");
                    _lessonItemId = value;
                    NotifyPropertyChanged("LessonItemId");
                }
            }
        }

        // Define item name: private field, public property, and database column.
        private string _lessonName;

        [Column]
        public string LessonName
        {
            get { return _lessonName; }
            set
            {
                if (_lessonName != value)
                {
                    NotifyPropertyChanging("LessonName");
                    _lessonName = value;
                    NotifyPropertyChanged("LessonName");
                }
            }
        }

        // Define item name: private field, public property, and database column.
        private string _lessonContent;

        [Column]
        public string LessonContent
        {
            get { return _lessonContent; }
            set
            {
                if (_lessonContent != value)
                {
                    NotifyPropertyChanging("LessonContent");
                    _lessonContent = value;
                    NotifyPropertyChanged("LessonContent");
                }
            }
        }

        // Define item name: private field, public property, and database column.
        private string _lessonLogo;

        [Column]
        public string LessonLogo
        {
            get { return _lessonLogo; }
            set
            {
                if (_lessonLogo != value)
                {
                    NotifyPropertyChanging("LessonLogo");
                    _lessonLogo = value;
                    NotifyPropertyChanged("LessonLogo");
                }
            }
        }

        // Define item name: private field, public property, and database column.
        private double _score;

        [Column]
        public double Score
        {
            get { return _score; }
            set
            {
                if (_score != value)
                {
                    NotifyPropertyChanging("Score");
                    _score = value;
                    NotifyPropertyChanged("Score");
                }
            }
        }

        // Define completion value: private field, public property, and database column.
        private bool _isCompleted;

        [Column]
        public bool IsComplete
        {
            get { return _isCompleted; }
            set
            {
                if (_isCompleted != value)
                {
                    NotifyPropertyChanging("IsComplete");
                    _isCompleted = value;
                    NotifyPropertyChanged("IsComplete");
                }
            }
        }

        // Define completion value: private field, public property, and database column.
        private string _missPronounedWord;

        [Column]
        public string MissPronounedWord
        {
            get { return _missPronounedWord; }
            set
            {
                if (_missPronounedWord != value)
                {
                    NotifyPropertyChanging("MissPronounedWord");
                    _missPronounedWord = value;
                    NotifyPropertyChanged("MissPronounedWord");
                }
            }
        }

        private int _missLetters;

        [Column]
        public int MissLetters
        {
            get { return _missLetters; }
            set
            {
                if (_missLetters != value)
                {
                    NotifyPropertyChanging("MissLetters");
                    _missLetters = value;
                    NotifyPropertyChanged("MissLetters");
                }
            }
        }

        private int _totalLetters;

        [Column]
        public int TotalLetters
        {
            get { return _totalLetters; }
            set
            {
                if (_totalLetters != value)
                {
                    NotifyPropertyChanging("TotalLetters");
                    _totalLetters = value;
                    NotifyPropertyChanged("TotalLetters");
                }
            }
        }

        private int _currentIndex;

        [Column]
        public int CurrentIndex
        {
            get { return _currentIndex; }
            set
            {
                if (_currentIndex != value)
                {
                    NotifyPropertyChanging("CurrentIndex");
                    _currentIndex = value;
                    NotifyPropertyChanged("CurrentIndex");
                }
            }
        }

        // Version column aids update performance.
        [Column(IsVersion = true)]
        private Binary _version;


        // Internal column for the associated LessonCategory ID value
        [Column]
        internal int _categoryId;

        // Entity reference, to identify the LessonCategory "storage" table
        private EntityRef<LessonCategory> _category;

        // Association, to describe the relationship between this key and that "storage" table
        [Association(Storage = "_category", ThisKey = "_categoryId", OtherKey = "Id", IsForeignKey = true)]
        public LessonCategory Category
        {
            get { return _category.Entity; }
            set
            {
                NotifyPropertyChanging("Category");
                _category.Entity = value;

                if (value != null)
                {
                    _categoryId = value.Id;
                }

                NotifyPropertyChanging("Category");
            }
        }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }


    [Table]
    public class LessonCategory : INotifyPropertyChanged, INotifyPropertyChanging
    {

        // Define ID: private field, public property, and database column.
        private int _id;

        [Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
        public int Id
        {
            get { return _id; }
            set
            {
                NotifyPropertyChanging("Id");
                _id = value;
                NotifyPropertyChanged("Id");
            }
        }

        // Define category name: private field, public property, and database column.
        private string _name;

        [Column]
        public string Name
        {
            get { return _name; }
            set
            {
                NotifyPropertyChanging("Name");
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }

        // Version column aids update performance.
        [Column(IsVersion = true)]
        private Binary _version;

        // Define the entity set for the collection side of the relationship.
        private EntitySet<LessonItem> _lessons;

        [Association(Storage = "_lessons", OtherKey = "_categoryId", ThisKey = "Id")]
        public EntitySet<LessonItem> Lessons
        {
            get { return this._lessons; }
            set { this._lessons.Assign(value); }
        }


        // Assign handlers for the add and remove operations, respectively.
        public LessonCategory()
        {
            _lessons = new EntitySet<LessonItem>(
                new Action<LessonItem>(this.attach_ToDo),
                new Action<LessonItem>(this.detach_ToDo)
                );
        }

        // Called during an add operation
        private void attach_ToDo(LessonItem toDo)
        {
            NotifyPropertyChanging("LessonItem");
            toDo.Category = this;
        }

        // Called during a remove operation
        private void detach_ToDo(LessonItem toDo)
        {
            NotifyPropertyChanging("LessonItem");
            toDo.Category = null;
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion

    }



    [Table]
    public class WordTable : INotifyPropertyChanged, INotifyPropertyChanging
    {

        // Define ID: private field, public property, and database column.
        private int _id;

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int Id
        {
            get { return _id; }
            set
            {
                if (_id != value)
                {
                    NotifyPropertyChanging("Id");
                    _id = value;
                    NotifyPropertyChanged("Id");
                }
            }
        }

        // Define item name: private field, public property, and database column.
        private string _word;

        [Column]
        public string Word
        {
            get { return _word; }
            set
            {
                if (_word != value)
                {
                    NotifyPropertyChanging("Word");
                    _word = value;
                    NotifyPropertyChanged("Word");
                }
            }
        }

        // Define item name: private field, public property, and database column.
        private int _frequency;

        [Column]
        public int Frequency
        {
            get { return _frequency; }
            set
            {
                if (_frequency != value)
                {
                    NotifyPropertyChanging("Frequency");
                    _frequency = value;
                    NotifyPropertyChanged("Frequency");
                }
            }
        }

        // Define item name: private field, public property, and database column.
        private string _type;

        [Column]
        public string Type
        {
            get { return _type; }
            set
            {
                if (_type != value)
                {
                    NotifyPropertyChanging("Type");
                    _type = value;
                    NotifyPropertyChanged("Type");
                }
            }
        }

        // Define completion value: private field, public property, and database column.
        private bool _isLearned;

        [Column]
        public bool IsLearned
        {
            get { return _isLearned; }
            set
            {
                if (_isLearned != value)
                {
                    NotifyPropertyChanging("IsLearned");
                    _isLearned = value;
                    NotifyPropertyChanged("IsLearned");
                }
            }
        }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }




    [Table]
    public class Country : INotifyPropertyChanged, INotifyPropertyChanging
    {

        // Define ID: private field, public property, and database column.
        private int _id;

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int Id
        {
            get { return _id; }
            set
            {
                if (_id != value)
                {
                    NotifyPropertyChanging("Id");
                    _id = value;
                    NotifyPropertyChanged("Id");
                }
            }
        }

        // Define item name: private field, public property, and database column.
        private string _name;

        [Column]
        public string Name
        {
            get { return _name; }
            set
            {
                if (_name != value)
                {
                    NotifyPropertyChanging("Name");
                    _name = value;
                    NotifyPropertyChanged("Name");
                }
            }
        }

        // Define item name: private field, public property, and database column.
        private string _code;

        [Column]
        public string Code
        {
            get { return _code; }
            set
            {
                if (_code != value)
                {
                    NotifyPropertyChanging("Code");
                    _code = value;
                    NotifyPropertyChanged("Code");
                }
            }
        }

        // Define item name: private field, public property, and database column.
        private string _language;

        [Column]
        public string language
        {
            get { return _language; }
            set
            {
                if (_language != value)
                {
                    NotifyPropertyChanging("language");
                    _language = value;
                    NotifyPropertyChanged("language");
                }
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }



    [Table]
    public class User : INotifyPropertyChanged, INotifyPropertyChanging
    {

        // Define ID: private field, public property, and database column.
        private int _id;

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int Id
        {
            get { return _id; }
            set
            {
                if (_id != value)
                {
                    NotifyPropertyChanging("Id");
                    _id = value;
                    NotifyPropertyChanged("Id");
                }
            }
        }

        // Define item name: private field, public property, and database column.
        private string _userID;

        [Column]
        public string UserID
        {
            get { return _userID; }
            set
            {
                if (_userID != value)
                {
                    NotifyPropertyChanging("UserID");
                    _userID = value;
                    NotifyPropertyChanged("UserID");
                }
            }
        }

        // Define item name: private field, public property, and database column.
        private string _email;

        [Column]
        public string Email
        {
            get { return _email; }
            set
            {
                if (_email != value)
                {
                    NotifyPropertyChanging("Email");
                    _email = value;
                    NotifyPropertyChanged("Email");
                }
            }
        }

        // Define item name: private field, public property, and database column.
        private string _languageCode;

        [Column]
        public string LanguageCode
        {
            get { return _languageCode; }
            set
            {
                if (_languageCode != value)
                {
                    NotifyPropertyChanging("LanguageCode");
                    _languageCode = value;
                    NotifyPropertyChanged("LanguageCode");
                }
            }
        }

        // Define item name: private field, public property, and database column.
        private bool _speakerGender;

        [Column]
        public bool SpeakerGender
        {
            get { return _speakerGender; }
            set
            {
                if (_speakerGender != value)
                {
                    NotifyPropertyChanging("SpeakerGender");
                    _speakerGender = value;
                    NotifyPropertyChanged("SpeakerGender");
                }
            }
        }

        // Define item name: private field, public property, and database column.
        private DateTime _lastLogin;

        [Column]
        public DateTime LastLogin
        {
            get { return _lastLogin; }
            set
            {
                if (_lastLogin != value)
                {
                    NotifyPropertyChanging("LastLogin");
                    _lastLogin = value;
                    NotifyPropertyChanged("LastLogin");
                }
            }
        }

        // Define item name: private field, public property, and database column.
        private double _score;

        [Column]
        public double Score
        {
            get { return _score; }
            set
            {
                if (_score != value)
                {
                    NotifyPropertyChanging("Score");
                    _score = value;
                    NotifyPropertyChanged("Score");
                }
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }
}
